/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ejecicios5_elegir;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.Scanner;

/**
 *
 * @author Mañana
 */
public class Ejecicios5_Elegir {

    private static Scanner sc = new Scanner(System.in);

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) throws FileNotFoundException, IOException {
        try {
            //Declaramos el Archivo
            //Buscamos el Fichero que queremos mostrar
            File newTextFile = new File("./QUIJOTE.txt");
            BufferedReader fr = new BufferedReader(new FileReader(newTextFile));
            System.out.println("Introduzca Palabra a Buscar");
            Scanner sc = new Scanner(System.in);
            String palabra = sc.next();
            int i = 0;
            int cont = 0;
            int palabraIgual = 0;
            String pal = " ";
            boolean leopal = false;
            while ((i = fr.read()) != -1) {
                if (Character.isLetter((char) i)) {
                    if (leopal) {
                        pal += (char) i;
                    } else {
                        leopal = true;
                        pal += (char) i;
                    }

                } else {
                    if (leopal) {
                        leopal = false;
                        cont++;
                        if (pal.equalsIgnoreCase(palabra)) {
                            palabraIgual++;
                        }
                        pal = "";
                    } else {
                    }
                }
            }
            fr.close();

            System.out.println("Palabras: " + cont);
            System.out.println("Palabras iguales a "+palabra+" :" + palabraIgual);

        } catch (IOException ex) {
            System.out.println("Excepcion de IO.");
        }
    }
}
